﻿using d1Common.WebController;
using d1Project.Modules.Business.doXZ.Models;
using d1Project.Tools;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Business.doXZ.Controllers.a1
{
    /// <summary>
    /// 学历类型
    /// </summary>
    [Route("a1/a1_Education")]
    public class a1_EducationController : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private doXZDbContext db = new doXZDbContext();

        /// <summary>
        /// 学历信息列表/下拉显示
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Get(XZ_XlTypeModel_GetVm model)
        {
            var entity = db.XZ_XlType.AsQueryable();
            if (model != null)
            {

            }
            var result = await entity.OrderByDescending(p => p.Id).ToPagedListAsync(model);
            return Ok(new { total = entity.Count(), rows = result });
        }

        /// <summary>
        /// 学历类型添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> Post(XZ_XlTypeModel_PostVm model)
        {
            if (!ModelState.IsValid) this.Error("请求参数不正确");
            XZ_XlTypeModel entity = new XZ_XlTypeModel();
        
            entity.XwName = model.XwName;
            db.XZ_XlType.Add(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }

        /// <summary>
        /// 删除学历类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Delete(int id)
        {
            var entity = await db.XZ_XlType.FindAsync(id);
            db.XZ_XlType.Remove(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }
    }
    public class XZ_XlTypeModel_GetVm : VmBase
    {

    }

    public class XZ_XlTypeModel_PostVm : VmBase
    {
        /// <summary>
        /// 学位名称
        /// </summary>
        public string XwName { get; set; }
    }
}
